package example.com.zyfypt_618zgm.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import retrofit2.Call;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.adapter.UserAdapter;
import example.com.zyfypt_618zgm.model.AddCollectModel;
import example.com.zyfypt_618zgm.model.AddFocusModel;
import example.com.zyfypt_618zgm.model.DelCollectModel;
import example.com.zyfypt_618zgm.model.DelFocusModel;
import example.com.zyfypt_618zgm.model.IsCollectModel;
import example.com.zyfypt_618zgm.model.IsFocusModel;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class UserVideo extends AppCompatActivity {
    private VideoView videoView;
    private String Url;
    private MenuItem item1;
    private MenuItem item2;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);
        videoView = (VideoView) findViewById(R.id.videoview);
        Url="http://amicool.neusoft.edu.cn/Uploads/video/video/"+ UserAdapter.videopath2;
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        videoView.setLayoutParams(layoutParams);
        videoView.setVideoPath(Url);
        videoView.setMediaController(new MediaController(this));
        videoView.requestFocus();
        videoView.start();
        collectcall(3);
        collectcall(6);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.xiaojiemian, menu);
        item1=menu.findItem(R.id.item1);
        item2=menu.findItem(R.id.item2);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
            case R.id.item1://监听菜单按钮
                if(item1.getTitle().equals("关注"))
                    collectcall(4);
                else if(item1.getTitle().equals("取消关注"))
                    collectcall(5);
                return true;
            case R.id.item2://监听菜单按钮
                if(item2.getTitle().equals("收藏"))
                    collectcall(1);
                else if(item2.getTitle().equals("取消收藏"))
                    collectcall(2);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void collectcall(final int flag){
        Call<String> call=null;
        if(flag==1){
            AddCollectModel AddCollectModel = new AddCollectModel();
            call=AddCollectModel.getResult(UserAdapter.mod2,UserAdapter.id3,Denglu.SessionID);
        }else if(flag==2){
            DelCollectModel DelCollectModel = new DelCollectModel();
            call=DelCollectModel.getResult(UserAdapter.mod2,UserAdapter.id3,Denglu.SessionID);
        }else if(flag==3){
            IsCollectModel IsCollectModel = new IsCollectModel();
            call= IsCollectModel.getResult(UserAdapter.mod2,UserAdapter.id3,Denglu.SessionID);
        }else if(flag==4){
            AddFocusModel AddFocusModel = new AddFocusModel();
            call= AddFocusModel.getResult("userfocus",UserAdapter.id4,Denglu.SessionID);
        }else if(flag==5){
            DelFocusModel DelFocusModel = new DelFocusModel();
            call= DelFocusModel.getResult("userfocus",UserAdapter.id4,Denglu.SessionID);
        }else if(flag==6){
            IsFocusModel IsFocusModel = new IsFocusModel();
            call=IsFocusModel.getResult("userfocus",UserAdapter.id4,Denglu.SessionID);
        }
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&& response.body() != null){
                    if(response.body().equals("0")){
                        if(flag==1){
                            Toast.makeText(UserVideo.this,"收藏失败",Toast.LENGTH_SHORT).show();//收藏失败
                        }else if(flag==2){
                            Toast.makeText(UserVideo.this,"取消收藏失败",Toast.LENGTH_SHORT).show();//取消收藏失败
                        }else if(flag==3){
                            item2.setTitle("取消收藏");//已收藏
                        }else if(flag==4){
                            Toast.makeText(UserVideo.this,"关注失败",Toast.LENGTH_SHORT).show();//关注失败
                        }else if(flag==5){
                            Toast.makeText(UserVideo.this,"取消关注失败",Toast.LENGTH_SHORT).show();//取消关注失败
                        }else if(flag==6){
                            item1.setTitle("取消关注");//已关注
                        }
                    }
                    else {
                        if(flag==1){
                            item2.setTitle("取消收藏");//收藏成功
                        }else if(flag==2){
                            item2.setTitle("收藏");//取消收藏成功
                        }else if(flag==3){
                            item2.setTitle("收藏");//未收藏
                        }else if(flag==4){
                            item1.setTitle("取消关注");//关注成功
                        }else if(flag==5){
                            item1.setTitle("关注");//取消关注成功
                        }else if(flag==6){
                            item1.setTitle("关注");//未关注
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(UserVideo.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
